package MulticastQuene;

import java.net.InetSocketAddress;
import java.util.Set;

public class MulticastMessageTotal extends MulticastMessage implements Comparable<MulticastMessageTotal> {
	private int clock;
	private Set<InetSocketAddress> sentTo;
    private int acknowledgesNeeded;
	
	public MulticastMessageTotal(InetSocketAddress sender, Set<InetSocketAddress> _sentTo, int _clock) {
		super(sender);
		clock = _clock;
		sentTo = _sentTo;
        if(_sentTo == null){
            acknowledgesNeeded = 0;
        }
        else{
            acknowledgesNeeded = _sentTo.size();
        }
	}
	
	public int getClock(){
		return clock;
	}
	public Set<InetSocketAddress> getSentTo(){
		return sentTo;
	}
    public void acknowledgeGotten(){
        acknowledgesNeeded--;
    }
    public void setAcknowledges(int v){
        acknowledgesNeeded = v;
    }
    public boolean hasBeenAcknowledged(){
        return acknowledgesNeeded == 0;
    }
	
	public int compareTo(MulticastMessageTotal msg) {
		if(msg.getClock() == clock){
			return msg.toString().compareTo(toString()); //Vil bare ikke have 0, medmindre det er den samme.
		}
		if(msg.getClock() < clock){		//TODO vende om?
			return 1;
		}
		return -1;
	}
	
	public boolean equals(Object o){
        if(o == null) return false;
        if(o instanceof MulticastMessageTotal){
            //Beskeder er ens hvis de er samme type besked fra samme sender på samme tidspunkt.
            MulticastMessageTotal msg = (MulticastMessageTotal) o;
            return (msg.toString().equals(toString())) && (clock == msg.getClock());
        }
        return false;
	}

    @Override
    public int hashCode() {
        int result = clock;
        result = 31 * result + (sentTo != null ? sentTo.hashCode() : 0);
        result = 17 * result + toString().hashCode();
        return result;
    }
}
